project(bandwidth
	VERSION "1.14.10"
	LANGUAGES C
)

# Config (bandwidth)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wno-sign-compare -Wno-type-limits -Wno-strict-prototypes -Wno-unused-parameter")

if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
	set(NASM_FLAGS_DEBUG "-gdwarf")
endif(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")

# Build (bandwidth)
check_language(ASM_NASM)
if(CMAKE_ASM_NASM_COMPILER)
	enable_language(ASM_NASM)
	set(BANDWIDTH_FOUND ON PARENT_SCOPE)
	message(STATUS "Using built-in ${PROJECT_NAME}, version ${PROJECT_VERSION}, object format ${CMAKE_ASM_NASM_OBJECT_FORMAT}")

	# NASM routines build
	if(CMAKE_ASM_NASM_OBJECT_FORMAT MATCHES "elf64" OR CMAKE_ASM_NASM_OBJECT_FORMAT MATCHES "elfx32")
		set(ROUTINES "routines-x86-64bit")
		set(UTILITY  "utility-x86-64bit")
	else()
		set(ROUTINES "routines-x86-32bit")
		set(UTILITY  "utility-x86-32bit")
	endif()
	add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ROUTINES}.o
		COMMAND ${CMAKE_ASM_NASM_COMPILER} ${NASM_FLAGS_DEBUG} -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} ${ROUTINES}.asm -o ${CMAKE_CURRENT_BINARY_DIR}/${ROUTINES}.o
		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
	)
	add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${UTILITY}.o
		COMMAND ${CMAKE_ASM_NASM_COMPILER} ${NASM_FLAGS_DEBUG} -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} ${UTILITY}.asm -o ${CMAKE_CURRENT_BINARY_DIR}/${UTILITY}.o
		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/OOC
	)

	if(LIBCPUID_FOUND AND WITH_LIBCPUID)
		add_definitions(-DHAS_LIBCPUID=1)
	else(LIBCPUID_FOUND AND WITH_LIBCPUID)
		add_definitions(-DHAS_LIBCPUID=0)
	endif(LIBCPUID_FOUND AND WITH_LIBCPUID)

	file(GLOB OOC_FILES
		"OOC/*.c"
		"OOC/*.h"
	)
	add_library(bandwidth
		STATIC
		${OOC_FILES}
		main.c
		libbandwidth.h
		Testing.c
		TestingX86.c
		${ROUTINES}.o
		${UTILITY}.o
	)

	target_link_libraries(bandwidth)
	set(BANDWIDTH_VERSION ${PROJECT_VERSION} PARENT_SCOPE)

else(CMAKE_ASM_NASM_COMPILER)
	set(BANDWIDTH_FOUND OFF PARENT_SCOPE)
endif(CMAKE_ASM_NASM_COMPILER)
